package com.cloudptt.api.product.func;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.pjsua2.api.IPjsuaService;
import com.cloudptt.api.product.func.sys.SysStateInfo;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.ringer.PromptTone;
import com.cloudptt.api.product.ringer.SoundPlayer;

import android.content.Context;
import android.media.MediaPlayer;

public class SipOperator implements MediaPlayer.OnCompletionListener
{
	private final static String THIS_FILE = "SipOperator";
	protected boolean isUpdate = false;
	protected int curStatus = SysStateInfo.SysState_SvrStopState;
	protected IPjsuaService service = null;
	protected PromptTone promptTone = null;
	protected SoundPlayer soundPlayer = null;
	protected boolean isOnLine = true;
	protected int publishResult = 0;

	public SipOperator(Context ctx)
	{
		Log.d(THIS_FILE, "SipOperator");
		this.soundPlayer = new SoundPlayer(ctx,this);
	}
	public void setPromptTone(PromptTone promptTone) 
	{
		this.promptTone = promptTone;
	}

	public void setService(IPjsuaService service) {
		this.service = service;
	}

	public void setOperatorStatue(int status)
	{
		Log.d(THIS_FILE, "setOperatorStatue status = " + status);
		this.curStatus = status;
	}

	public boolean isUpdate() {
		return isUpdate;
	}

	public void setUpdate(boolean isUpdate) {
		this.isUpdate = isUpdate;
	}

	protected int checkServiceNotNull()
	{
		if (service == null) 
		{
			return CauseCont.OperateCheck_Sys_ServiceNull;
		}
		return CauseCont.OperateCheck_Suc;
	}

	protected int checkFirstForLogin()
	{
		if (service == null) 
		{
			return CauseCont.OperateCheck_Sys_ServiceNull;
		}
		if(curStatus == SysStateInfo.SysState_SvrStopState)
		{
			return CauseCont.OperateCheck_Sys_SvrStop;
		}
		if(isUpdate())
		{
			return CauseCont.OperateErr_Update;
		}
		if(!isOnLine)
		{
			return CauseCont.OperateErr_Offline;
		}
		return CauseCont.OperateCheck_Suc;
	}
	protected int checkFirstForUser()
	{
		if(curStatus == SysStateInfo.SysState_SvrStartState)
		{
			return CauseCont.OperateCheck_Sys_SvrStart;
		}
		else if(curStatus == SysStateInfo.SysState_SipAuthingState)
		{
			return CauseCont.OperateCheck_Sys_InLogining;
		}
//		else if(curStatus != SysStateInfo.SysState_SipPulishSucState)
//		{
//			return CauseCont.OperateCheck_Sys_UnLogin;
//		}

		if(!isOnLine())
		{
			return CauseCont.OperateCheck_Sys_LinkDisconnect;
		}
		if(isUpdate())
		{
			return CauseCont.OperateErr_Update;
		}
		return CauseCont.OperateCheck_Suc;
	}


	protected int checkFirst()
	{
		int result = checkFirstForLogin();
		if (result != CauseCont.OperateCheck_Suc) 
		{
			return result;
		}
		result = checkFirstForUser();
		if (result != CauseCont.OperateCheck_Suc) 
		{
			return result;
		}
		return CauseCont.OperateCheck_Suc;
	}

	public int checkOperate()
	{
		int result = checkFirstForLogin();
		if (result != CauseCont.OperateCheck_Suc) 
		{
			return result;
		}
		result = checkFirstForUser();
		if (result != CauseCont.OperateCheck_Suc) 
		{
			return result;
		}
		return result;
	}

	public void startSoundPlayer(int type)
	{
		Log.d(THIS_FILE, "startSoundPlayer type = " + type);
		if(promptTone != null
				&& promptTone.canPlay(type))
		{
			promptTone.soundRing(type);
		}
		else
		{
			int result = checkFirst();
			Log.d(THIS_FILE, "startSoundPlayer checkFirstForUser = " + result + " promptTone = " + promptTone);
			if(CauseCont.OperateCheck_Suc == result
					&& service != null)
			{
				this.soundPlayer.soundRing( type );
			}
			else if(promptTone != null
					&& CauseCont.OperateErr_Update == result)
			{
				Log.d(THIS_FILE, "startSoundPlayer updateWarring ");
				promptTone.soundRing(PromptTone.Type_Upgrad);
			}
			else if(promptTone != null
					&& isNoLink(result))
			{
				Log.d(THIS_FILE, "startSoundPlayer updateWarring ");
				promptTone.soundRing(PromptTone.Type_NoLink);
			}
//			else if(promptTone != null
//					&& !promptTone.isBtnUp(type))
//			{
//				Log.d(THIS_FILE, "startSoundPlayer disscontWarring ");
//				promptTone.soundRing(PromptTone.Type_Disconnect);
//			}
		}
	}
	private boolean isNoLink(int result)
	{
		if(CauseCont.OperateCheck_Sys_AccountNull == result
				|| CauseCont.OperateCheck_Sys_UnLogin == result
				|| CauseCont.OperateCheck_Sys_InLogining == result
				|| CauseCont.OperateCheck_Sys_LinkDisconnect == result)
		{
			return true;
		}
		return false;
	}

	public void setPublishResult(int publishResult) {
		this.publishResult = publishResult;
	}

	public boolean checkPublishResult(int type)
	{
		Log.d(THIS_FILE, "checkPublishResult type = " + type + " publishResult= " + publishResult);
		boolean result = (publishResult & type) != 0;
		Log.d(THIS_FILE, "checkPublishResult result = " + result);
		if((publishResult & type) != 0)
		{
			return true;
		}
		return false;
	}
	public boolean isOnLine() {
		return isOnLine;
	}
	public void setOnLine(boolean isOnLine) {
		this.isOnLine = isOnLine;
	}

	public void clearResource()
	{
		this.service = null;
	}
	@Override
	public void onCompletion(MediaPlayer mp) {
		// TODO Auto-generated method stub

	}
}
